import {GetCommentByBlogId} from '../../request/api'
import { getUserInfoById } from '@/utils'
export default{
    namespaced:true,
    state:{
        commentList:[],
        userComment:[]
    },
    mutations:{
        setCommentList(state,payload){
            state.commentList=payload
        },
        setUserComment(state,payload){
            state.userComment=payload
        }
    },
    actions:{
        async getFetchCommentList({commit},params){
            const commentList=[]
            let res = await GetCommentByBlogId(params)
            // console.log("action：",res.data.data)
            const comments = res.data.data
            for(const item of comments){
                // 一级评论
                if(item.istop == "1"){
                    // 根据userid请求用户数据
                    // 封装commentuser
                    const commentUser1={
                            id:null,
                            nickName:'',
                            avatar:''
                    }
                    getUserInfoById(item.commentuserid).then(result =>{
                        commentUser1.id = result.userid
                        commentUser1.nickName = result.name
                        commentUser1.avatar = require('../../assets/avator/'+result.avator)
                    })
                    // 封装一级评论
                    const comment={
                        id:null,
                        commentUser:{},
                        targetUser:{},
                        content:'',
                        createDate:'',
                        childrenList:[]
                    }
                    comment.id = item.commentid
                    comment.commentUser = commentUser1
                    comment.content = item.content
                    comment.createDate = item.createdate
                    // 封装完成，push
                    commentList.unshift(comment)
                }else if(item.istop == "0"){
                    // 二级评论先分类
                    // 根据userid请求用户数据
                    // 封装commentuser
                    const commentUser={
                            id:null,
                            nickName:'',
                            avatar:''
                    }
                    getUserInfoById(item.commentuserid).then(result =>{
                        commentUser.id = result.userid
                        commentUser.nickName = result.name
                        commentUser.avatar = require('../../assets/avator/'+result.avator)
                    })
                    // targetUser
                    const targetUser={
                        id:null,
                        nickName:''
                    }
                    // 获取回复的用户信息
                    getUserInfoById(item.targetuserid).then(result =>{
                        targetUser.id = result.userid
                        targetUser.nickName = result.name
                    })
                    // 封装成二级评论
                    const commentTwo={
                        id:null,
                        commentUser:{},
                        targetUser:{},
                        content:'',
                        createDate:'',
                        whichcomment:null
                    }
                    commentTwo.id = item.commentid
                    commentTwo.commentUser = commentUser
                    commentTwo.targetUser = targetUser
                    commentTwo.content = item.content
                    commentTwo.createDate = item.createdate
                    commentTwo.whichcomment = item.whichcommentid
                    // 分类保存到一级评论中---将二级评论中的whichcomment 与 id比较
                    commentList.forEach(item =>{
                        if(commentTwo.whichcomment == item.id){
                            item.childrenList.unshift(commentTwo)
                        }
                    })
                }
            }

            commit('setCommentList',commentList)
        }
    }
}